<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Send;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class SendController extends AdminController
{
    protected $title = '短信发送历史列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Send(), function (Grid $grid) {
            $grid->model()->orderBy("id", "desc");
            $grid->id->sortable();
            $grid->title;
            $grid->content;
            $grid->admin_id->display(function ($id) {
                return DB::table('admin_users')->find($id)->username;
            });
            $grid->type->display(function ($value) {
                return \App\Models\Send::$mapTYPE[$value];
            });
            $grid->has_exception("是否发送失败")->display(function ($value) {
                return $value ? "是" : "否";
            });
            $grid->exception_msg("失败原因");
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->disableBatchDelete();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Send(), function (Show $show) {
            $show->id;
            $show->title;
            $show->content;
            $show->admin_id;
            $show->type;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Send(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('content');
            $form->text('admin_id');
            $form->text('type');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
